package net.maku.module.service.impl;

import net.maku.framework.common.utils.Result;
import net.maku.module.entity.AgThirdParty;
import net.maku.module.dao.AgThirdPartyDao;
import net.maku.module.query.AgThirdPartyQuery;
import net.maku.module.service.AgThirdPartyService;
import org.springframework.stereotype.Service;

import jakarta.annotation.Resource;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 三方平台地址管理(AgThirdParty)表服务实现类
 *
 * @author ChiHongYu
 * @since 2025-01-09 10:06:46
 */
@Service("agThirdParty")
public class AgThirdPartyServiceImpl implements AgThirdPartyService {
    @Resource
    private AgThirdPartyDao agThirdPartyDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public AgThirdParty queryById(Integer id) {
        return this.agThirdPartyDao.queryById(id);
    }


    @Override
    public List<AgThirdParty> queryByLimit(Map<String, Object> param, int limit1, int limit2) {
        return this.agThirdPartyDao.page(param, limit1, limit2);
    }

    @Override
    public int queryCount(Map<String, Object> param) {
        return this.agThirdPartyDao.count(param);
    }

    /**
     * 新增数据
     *
     * @param agThirdParty 实例对象
     * @return 是否成功
     */
    @Override
    public boolean insert(AgThirdParty agThirdParty) {
        return this.agThirdPartyDao.insert(agThirdParty) > 0;
    }

    /**
     * 修改数据
     *
     * @param agThirdParty 实例对象
     * @return 是否成功
     */
    @Override
    public boolean update(AgThirdParty agThirdParty) {
        return this.agThirdPartyDao.update(agThirdParty) > 0;
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.agThirdPartyDao.deleteById(id) > 0;
    }

    /**
     * 通过主键修改使用状态
     *
     * @param id       主键
     * @param useState 使用状态
     * @return 是否成功
     */
    @Override
    public boolean changeUseById(Integer id, String useState) {
        return this.agThirdPartyDao.changeUseById(id, useState) > 0;
    }

    @Override
    public Result<AgThirdParty> createOrUpdate(AgThirdPartyQuery param, long executor) {
        if (param.getId() != null && !param.getId().equals("")) {
            AgThirdParty thirdParty = agThirdPartyDao.queryById(param.getId());
            if (thirdParty == null)
                return Result.error(400, "参数错误，请检查id");
            else {
                param.makeUpdateEntity(thirdParty, executor);
                if (this.update(thirdParty))
                    return Result.ok(thirdParty);
                else
                    return Result.error(500, "服务器错误，请联系管理员");
            }
        } else {

            AgThirdParty thirdParty = param.makeCreateEntity(executor);
            thirdParty.setId(param.getId());
            if (this.insert(thirdParty))
                return Result.ok(thirdParty);
            else
                return Result.error(500, "服务器错误，请联系管理员");
        }
    }

    @Override
    public List<AgThirdParty> getList(HashMap<String, Object> param) {
        return agThirdPartyDao.getList(param);
    }

}
